package com.egao.go.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.go.biz.DutchUserBiz;
import com.egao.go.entity.DutchUser;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-09-20 17:25:43
 * @version ：1.0
 */
@Repository
public class DutchUserBizImpl extends BaseDao implements DutchUserBiz {

	@Override
	public JsonResult list(PageResult2<T> page, DutchUser dutchUser, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DutchUser> rm = new BeanPropertyRowMapper<DutchUser>(DutchUser.class);
		StringBuffer sql = new StringBuffer("select d.id,d.dutchId,d.openid");
        sql.append(",d.gName,d.gAvatarUrl,d.gCommnets,d.price");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from go_dutch_user d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" where d.deleted=0  ");
    	if(Utils.isNotEmpty(dutchUser)){
//			if(Utils.isNotEmpty(dutchUser.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", dutchUser.getName());
//			}
			if(Utils.isNotEmpty(dutchUser.getSort())&&Utils.isNotEmpty(dutchUser.getOrder())){
                sql.append(" order by d."+dutchUser.getSort()+" "+dutchUser.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<DutchUser> list(DutchUser dutchUser, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DutchUser> rm = new BeanPropertyRowMapper<DutchUser>(DutchUser.class);
        StringBuffer sql = new StringBuffer("select d.id,d.dutchId,d.openid");
        sql.append(",d.gName,d.gAvatarUrl,d.gCommnets,d.price");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from go_dutch_user d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" where d.deleted=0 and d.dutchId=:dutchId ");
		map.put("dutchId", dutchUser.getDutchid()+"");

    	if(Utils.isNotEmpty(dutchUser)){
//			if(Utils.isNotEmpty(dutchUser.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", dutchUser.getName());
//			}
            if(Utils.isNotEmpty(dutchUser.getSort())&&Utils.isNotEmpty(dutchUser.getOrder())){
				sql.append(" order by d."+dutchUser.getSort()+" "+dutchUser.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public DutchUser getById(DutchUser dutchUser) {
		RowMapper<DutchUser> rm = new BeanPropertyRowMapper<DutchUser>(DutchUser.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.dutchId,d.openid");
        sql.append(",d.gName,d.gAvatarUrl,d.gCommnets,d.price");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from go_dutch_user d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", dutchUser.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Integer save(DutchUser dutchUser, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into go_dutch_user (");
        sql.append("dutchId,openid,gName,gAvatarUrl");
		sql.append(",gCommnets,price,create_user,create_time) values ( ");
        sql.append(":dutchId,:openid,:gName,:gAvatarUrl");
		sql.append(",:gCommnets,:price,:create_user,now())");
        map.put("dutchId", dutchUser.getDutchid()==null?null:dutchUser.getDutchid()+"");
        map.put("openid", loginUser.getXcxopenid());
        map.put("gName", loginUser.getNickName());
        map.put("gAvatarUrl", loginUser.getAvatar());
        map.put("gCommnets", dutchUser.getGcommnets());
        map.put("price", dutchUser.getPrice());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(DutchUser dutchUser, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update go_dutch_user set ");
        sql.append("dutchId=:dutchId,openid=:openid,gName=:gName ");
		sql.append(",gAvatarUrl=:gAvatarUrl,gCommnets=:gCommnets,price=:price ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("dutchId", dutchUser.getDutchid()==null?null:dutchUser.getDutchid()+"");
        map.put("openid", dutchUser.getOpenid());
        map.put("gName", dutchUser.getGname());
        map.put("gAvatarUrl", dutchUser.getGavatarurl());
        map.put("gCommnets", dutchUser.getGcommnets());
        map.put("price", dutchUser.getPrice());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", dutchUser.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(DutchUser dutchUser, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(dutchUser.getId().length()>0){
			for(String ID : dutchUser.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update go_dutch_user set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
